﻿using System;
using System.IO;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Web;
using System.Text;

using WebDavProcessor;
using WebDavProcessor.Interfaces;

namespace WCFServer.Helpers
{
    static class Helper
    {
        public static IWebDavRequest ToWebDavRequest(this HttpRequestMessageProperty request, string file, Stream str)
        {
            WebDavRequest davRequest = new WebDavRequest();
            davRequest.Method = request.Method;
            davRequest.RequestUri = new Uri(file, UriKind.Relative);
            davRequest.Headers = new System.Collections.Specialized.NameValueCollection();
            foreach (string header in request.Headers.Keys)
                davRequest.Headers.Add(header, request.Headers[header]);

            davRequest.InputStream = str;

            return davRequest;
        }

        public static void ParseResourceNameLock(this string resource, out string resourceName, out string lockID)
        {
            string[] str = resource.Split(':');
            string[] tmp = new string[2];
            if (str.Length > 3)
                throw new Exception("icorrect resource name format");
            if (str.Length == 1)
            {
                lockID = string.Empty;
                resourceName = resource;
            }
            else
            {
                lockID = str[0];
                resourceName = str[1];
            }
        }

        
    }
}
